﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Drawing.Text;
using System.Drawing;


public partial class Receipt : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                string barcode = Request.QueryString["barcode"];
                bindPackageDetail(barcode);
                //if (!string.IsNullOrEmpty(filename))
                //{
                //    ShowPdf(Server.MapPath("~/Receipt/" + filename + ".pdf"));
                //}
            }
            catch { ;}
            //string barcode = Request.QueryString["barcode"];
            //if (!string.IsNullOrEmpty(barcode))
            //{
            //    bindPackageDetail(barcode);
            //}
            //else
            //{
            //    Response.Redirect("~/Staff/ScanReceipt.aspx");
            //}

            
        }
        //PrivateFontCollection p = new PrivateFontCollection();
        //p.AddFontFile(Server.MapPath("~/Styles/fre3of9x.ttf"));
        //FontFamily ff = p.Families[0];
        //System.Drawing.Font oFont = new System.Drawing.Font(ff, 24);
        //Label l = dlPackageDetail.FindControl("Label9") as Label;
        ////if (l != null)
        //{
        //    ;
        //}
    }

    private void ShowPdf(string filename)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "inline;filename=" + filename);
        Response.ContentType = "application/pdf";
        Response.WriteFile(filename);
        Response.Flush();
        Response.Clear();
    }

    private void bindPackageDetail(int packageID)
    {
        BLLConfirmShipment bll = new BLLConfirmShipment();
        dlPackageDetail.DataSource = new List<vPackageDetail>() { bll.getPackageInformation(packageID) };
        dlPackageDetail.DataBind();
    }

    private void bindPackageDetail(string barcode)
    {
        BLLConfirmShipment bll = new BLLConfirmShipment();
        tblPackage package = bll.getPackageByBarcode(barcode);
        if (package != null)
        {
            bindPackageDetail(package.ID);
        }
        else
        {
            Response.Redirect("~/NewShipment.aspx");
        }

    }
    protected void btnPrint_Click(object sender, EventArgs e)
    {
        // BLLShipment bllShipment = new BLLShipment();
        // bllShipment.GenerateReceipt(61, Server.MapPath("~/Receipt/61.pdf"));

        //StringWriter sw = new StringWriter();
        //HtmlTextWriter w = new HtmlTextWriter(sw);
        //divContent.RenderControl(w);

        //string content = sw.GetStringBuilder().ToString();


        //  bllShipment.GenerateReceipt2(content, Server.MapPath("~/Receipt/61)1.pdf"));
    }
}